package com.smedi.ismedi.web.starter.util;

import lombok.extern.slf4j.Slf4j;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Component;

import static cn.hutool.extra.pinyin.PinyinUtil.getFirstLetter;

/**
 * @author: xulinjun
 * @date: 2021-6-9 21:12
 */
@Slf4j
@Component
public class PinyinUtil {
    /**
     * 将中文转换为英文
     *
     * @param name 中文数据
     * @return result
     * @throws BadHanyuPinyinOutputFormatCombination
     * @author isstech
     * @date 2021-01-07 10:16
     */
    public String getEname(String name) throws BadHanyuPinyinOutputFormatCombination {
        HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
        pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);
        return PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
    }

    /**
     * 首字母打写
     *
     * @param name 参数中文字符串
     * @return result
     * @throws {@link BadHanyuPinyinOutputFormatCombination}
     * @author isstech
     * @date 2021-01-04 14:25
     */
    public String getUpFirstData(String name) throws BadHanyuPinyinOutputFormatCombination {
        HanyuPinyinOutputFormat pyFormat = new HanyuPinyinOutputFormat();
        pyFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        pyFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        pyFormat.setVCharType(HanyuPinyinVCharType.WITH_V);

        String result = PinyinHelper.toHanyuPinyinString(name, pyFormat, "");
        result = result.substring(0, 1).toUpperCase() + result.substring(1);
        return result;
    }

    /**
     * 姓、名的第一个字母需要为大写
     *
     * @param name 中文汉字
     * @return result
     * @throws BadHanyuPinyinOutputFormatCombination
     * @author isstech
     * @date 2021-01-07 10:17
     */
    public String getUpEname(String name) throws BadHanyuPinyinOutputFormatCombination {
        char[] strs = name.toCharArray();
        String newname = null;

        //名字的长度
        if (strs.length == 2) {
            newname = toUpCase(getEname("" + strs[0])) + " "
                    + toUpCase(getEname("" + strs[1]));
        } else if (strs.length == 3) {
            newname = toUpCase(getEname("" + strs[0])) + " "
                    + toUpCase(getEname("" + strs[1] + strs[2]));
        } else if (strs.length == 4) {
            newname = toUpCase(getEname("" + strs[0] + strs[1])) + " "
                    + toUpCase(getEname("" + strs[2] + strs[3]));
        } else {
            newname = toUpCase(getEname(name));
        }
        return newname;
    }

    /**
     * 首字母大写
     *
     * @param str 中文
     * @return result
     * @author isstech
     * @date 2021-01-07 10:17
     */
    private String toUpCase(String str) {
        StringBuffer newstr = new StringBuffer();
        newstr.append((str.substring(0, 1)).toUpperCase()).append(
                str.substring(1, str.length()));

        return newstr.toString();
    }

    public static String getSpells(String characters) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < characters.length(); i++) {

            char ch = characters.charAt(i);
            if ((ch >> 7) == 0) {
                // 判断是否为汉字，如果左移7为为0就不是汉字，否则是汉字
                buffer.append(ch);
            } else {
                char spell = getFirstLetter(ch);
                buffer.append(spell);
            }
        }
        return buffer.toString();
    }
}
